//
//  LSTabBar.m
//  5.TabBar中间按钮
//
//  Created by 梁森 on 2020/7/13.
//  Copyright © 2020 梁森. All rights reserved.
//

#import "LSTabBar.h"

@interface LSTabBar ()

@property (nonatomic, strong) UIButton * centerBtn;
@property (nonatomic, strong) UILabel * centerLbl;
@property (nonatomic, strong) UIImage * btnImg;
@property (nonatomic, copy) NSString * title;

@end

@implementation LSTabBar

//- (instancetype)initWithFrame:(CGRect)frame{
//    self = [super initWithFrame:frame];
//    if (self) {
//        [self addSubview:self.centerBtn];
//        [self addSubview:self.centerLbl];
//    }
//    return self;
//}
- (instancetype)initWithImage:(UIImage *)image andTitle:(NSString *)title{
    self = [super init];
    if (self) {
        self.translucent = NO;
        self.btnImg = image;
        self.title = title;
        [self addSubview:self.centerBtn];
        [self addSubview:self.centerLbl];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat tabBarBtnW = self.frame.size.width / (self.items.count+1);
    NSInteger tabBarIndex = 0;
    for (UIView *subView in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([subView isKindOfClass:class]) {
            CGRect frame = subView.frame;
            frame.size.width = tabBarBtnW;
            frame.origin.x = tabBarIndex * tabBarBtnW;
            subView.frame = frame;
            
            // 设置中间按钮和文字的位置
            if (tabBarIndex == 0) {
                for (UIView * imageLblV in subView.subviews) {
                    CGPoint center = self.center;
                    center.x  = self.frame.size.width * 0.5;
                    if ([imageLblV isKindOfClass:[UIImageView class]]) {
                        imageLblV.backgroundColor = [UIColor redColor];
                        center.y = imageLblV.center.y - (self.centerBtn.frame.size.height - imageLblV.frame.size.height) * 0.5;
                        self.centerBtn.center = center;
                        CGRect btnFrame = self.centerBtn.frame;
                        btnFrame.size.width = tabBarBtnW;
                        self.centerBtn.frame = btnFrame;
                    }else if ([imageLblV isKindOfClass:[UILabel class]]) {
                        center.y = imageLblV.center.y;
                        self.centerLbl.center = center;
                        UILabel * lbl = (UILabel *)imageLblV;
                        self.centerLbl.font = lbl.font;
                        imageLblV.backgroundColor = [UIColor yellowColor];
                    }
                }
            }
            CGFloat centerNum = self.items.count / 2;
            tabBarIndex++;
            if (tabBarIndex == centerNum) {
                tabBarIndex++;
            }
        }
        subView.backgroundColor = [UIColor greenColor];
    }
}

- (void)clickCenterBtn{
    NSLog(@"%s", __func__);
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    if (self.isHidden == NO) {
        CGPoint newP = [self convertPoint:point toView:self.centerBtn];
        if ( [self.centerBtn pointInside:newP withEvent:event]) {
            return self.centerBtn;
        }else{
            return [super hitTest:point withEvent:event];
        }
    }else {
        return [super hitTest:point withEvent:event];
    }
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return [super pointInside:point withEvent:event];
}

#pragma mark --- 懒加载
- (UIButton *)centerBtn{
    if (_centerBtn == nil) {
        _centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_centerBtn setImage:self.btnImg forState:UIControlStateNormal];
        _centerBtn.frame = CGRectMake(0, 0, _centerBtn.currentImage.size.width, _centerBtn.currentImage.size.height);
        [_centerBtn addTarget:self action:@selector(clickCenterBtn) forControlEvents:UIControlEventTouchUpInside];
        _centerBtn.backgroundColor = [UIColor greenColor];
    }
    return _centerBtn;
}
- (UILabel *)centerLbl{
    if (_centerLbl == nil) {
        _centerLbl = [UILabel new];
        _centerLbl.text = self.title;
        _centerLbl.textColor = [UIColor redColor];
        _centerLbl.textAlignment = NSTextAlignmentCenter;
        [_centerLbl sizeToFit];
    }
    return _centerLbl;
}

@end
